package  
{
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	/**
	 * ...
	 * @author ...
	 */
	public class MMCheckBox extends MMInteractive 
	{
		private var _label:MMLabel;
		private var _bgImage:MMImage;
		private var _statusImageList:Vector.<BitmapData> = new Vector.<BitmapData>(2);
		private var _labelText:String;
		private var _selected:uint;
		private var _padding:uint;
		public var onChange:Function;

		public function MMCheckBox(selectedImage:BitmapData,unSelectedImage:BitmapData) 
		{
			super();
			mouseChildren = true;
			_statusImageList[0] = selectedImage;
			_statusImageList[1] = unSelectedImage;
		}
		
		override public function onInit():void
		{
			super.onInit();
			_bgImage = new MMImage(_statusImageList[_selected]);
			_bgImage.buttonMode = true;
			_bgImage.show();
			_bgImage.onClick = clickHandler;
			addChild(_bgImage);
			
			_label = new MMLabel();
			_label.show();
			addChild(_label);
			
		}
		private function clickHandler(e:MouseEvent):void
		{
			selected = !selected;
			onChange && onChange.call(this);
		}
		
		override public function onHide():void
		{
			super.onHide();
		}
		
		override public function onShow():void
		{
			super.onShow();
			update();
		}
		
		override public function updateData():void
		{
			super.updateData();
			if (_bgImage.bitmapData != _statusImageList[_selected])
			{
				_bgImage.bitmapData = _statusImageList[_selected];
			}
			
			if (_label.text != _labelText)
			{
				_label.text = _labelText;
			}
			
			_label.place(_bgImage.width + _padding, (_bgImage.height - _label.height) / 2);
		}
		
		public function set label(text:String):void
		{
			_labelText = text;
			update();
		}
		
		public function get selected():Boolean { return Boolean(_selected); }
		
		public function set selected(value:Boolean):void 
		{
			var val:uint = uint(value);
			if (_selected != val)
			{
				_selected = val;
				update();
			}
			
		}
		
	}

}